<!-- 苗木详情 -->
<template>
  <div class="detail-container">
    <div class="nav-bar">
      <van-nav-bar
        title="苗木详情"
        left-arrow
        @click-left="onClickLeft"
        fixed
        placeholder
        safe-area-inset-top
      ></van-nav-bar>
    </div>
    <div class="main">
      <!-- 轮播图区域 -->
      <div class="swiper-section">
        <van-swipe class="swiper" :autoplay="3000" indicator-color="#fff">
          <van-swipe-item v-for="(image, index) in images" :key="index">
            <van-image :src="image" fit="cover" />
          </van-swipe-item>
        </van-swipe>
      </div>

      <!-- 内容区域 -->
      <div class="content-section">
        <!-- 价格区域 -->
        <div class="price-section">
          <div class="price">
            <div class="amount-area">
              <span class="symbol">¥</span>
              <span class="amount" v-if="seedingInfo.priceType == 2"
                >{{ seedingInfo.priceMin }}-{{ seedingInfo.priceMax }}元</span
              >
              <span class="amount" v-else>面议</span>
            </div>
            <div class="relese-time">发布时间: {{ seedingInfo?.createTime?.slice(0, 10) }}</div>
          </div>
          <div class="title">{{ seedingInfo.title }}</div>
        </div>

        <!-- 规格信息 -->
        <div class="spec-section">
          <div class="section-title">产品规格</div>
          <div class="spec-list">
            <div class="spec-item" v-for="item in seedingInfo.attributeDocuments">
              <span class="label">{{ item.attributeName }}</span>
              <span class="value">{{ item.minValue }}-{{ item.maxValue }}{{ item.unit }}</span>
            </div>
          </div>
        </div>

        <!-- 商家信息 -->
        <div class="merchant-section">
          <div class="merchant-info">
            <div class="avatar">
              <van-image :src="merchantInfo?.headUrl || avatar" round width="50" height="50" />
              <p v-if="merchantInfo?.authType == 1">实名认证</p>
            </div>
            <div class="info">
              <div class="name">
                {{ merchantInfo?.userCompanyAuthDTO?.companyName || '暂无公司名称' }}
              </div>
              <div class="tags">
                <van-tag
                  class="crown"
                  plain
                  type="primary"
                  size="medium"
                  v-if="merchantInfo?.vipLevelCode"
                  >{{ getVipLevel }}</van-tag
                >
                <van-tag
                  class="verify"
                  plain
                  type="primary"
                  size="medium"
                  v-if="merchantInfo?.companyAuthType == 1"
                  >企业认证</van-tag
                >
              </div>
              <div class="stars">
                <van-rate
                  v-model="value"
                  :size="16"
                  color="#ffd21e"
                  void-icon="star"
                  void-color="#eee"
                  readonly
                />
              </div>
            </div>
            <!-- <div class="contact">
              <van-button plain type="primary" size="small" round>逛一逛</van-button>
            </div> -->
          </div>
          <div class="merchant-desc van-hairline--bottom">
            <div class="desc-item">
              主营项目：{{ merchantInfo?.userCompanyAuthDTO?.businessCategory }}
            </div>
            <div class="desc-item">
              企业介绍：{{ merchantInfo?.userCompanyAuthDTO?.companyDesc }}
            </div>
          </div>
          <div class="merchant-fans">
            <div class="left">
              <van-icon
                @click="handleFavorite"
                :name="merchantInfo?.favoriteStoreState ? 'like' : 'like-o'"
              />粉丝{{ merchantInfo.favoriteNum }}
            </div>
            <div class="right">
              <van-button round type="default" @click="handlePhone">打电话</van-button>
            </div>
          </div>
        </div>

        <div class="merchant-section">
          <van-image width="100%" v-for="image in images" :src="image" fit="cover" />
        </div>

        <!-- 动态列表 -->
        <div class="dynamic-section">
          <div class="section-title">动态(99)</div>
          <div class="dynamic-list">
            <div class="dynamic-item" v-for="(item, index) in 4" :key="index">
              <div class="user-info">
                <van-image
                  src="https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg"
                  round
                  width="30"
                  height="30"
                />
                <div class="name-time">
                  <div class="name">张老板<span>拨打电话咨询</span></div>
                  <div class="time">刚刚</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部按钮区域 -->
    <div class="footer-section" :class="deviceType === 'ios' ? 'safe-area' : ''">
      <div class="left">
        <div class="btn-item" @click="handleStore">
          <van-icon size="20" :name="seedingInfo?.favoritePushState == 1 ? 'like' : 'like-o'" />
          <span>收藏</span>
        </div>
      </div>
      <div class="right">
        <van-button round type="default" @click="handlePhone">打电话</van-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { getDeviceType } from '@/utils/tools'
import { ref, computed } from 'vue'
import { getById, getMerchantInfo, favorite, unFavorite, getCompanyContactPhone } from '@/api'
import avatar from '../assets/avatar.png'
import query from '@/utils/query.js'
import bridge from '@/utils/bridge'
const route = useRoute()
const phone = ref('')
// 轮播图信息
const images = ref([])
const value = ref(5)
// 苗木信息
const seedingInfo = ref({})
// 苗商信息
const merchantInfo = ref({})
// 终端类型
const deviceType = ref('')

// 获取会员等级
const getVipLevel = computed<string>(() => {
  if (merchantInfo.value.vipLevelCode == 'v0') {
    return '普通会员'
  } else if (merchantInfo.value.vipLevelCode == 'v1') {
    return '个人会员'
  } else if (merchantInfo.value.vipLevelCode == 'v2') {
    return '企业会员'
  } else if (merchantInfo.value.vipLevelCode == 'v3') {
    return '白金会员'
  } else if (merchantInfo.value.vipLevelCode == 'v4') {
    return '钻石会员'
  } else if (merchantInfo.value.vipLevelCode == 'v5') {
    return '皇冠会员'
  }
})

// 获取苗商电话
const getPhone = async () => {
  let { code, data } = await getCompanyContactPhone({
    userId: merchantInfo.value.userId
  })
  if (code === 200) {
    phone.value = data
  }
}

const onClickLeft = () => {
  bridge.closeWebView()
}

onMounted(async () => {
  bridge.setNavigationBarVisible({
    value: '0'
  })
  deviceType.value = getDeviceType()
  await getDetailInfo()
  await getMxInfo()
})
// 苗木详情
const getDetailInfo = async () => {
  let { code, data } = await getById({
    pushProductId: route.query.pushProductId
  })
  if (code === 200) {
    seedingInfo.value = data
    images.value = seedingInfo.value.ossUrls.split(',')
  }
}
// 苗商信息
const getMxInfo = async () => {
  let { code, data } = await getMerchantInfo({
    userId: seedingInfo.value.userId
  })
  if (code === 200) {
    merchantInfo.value = data
  }
}
// 收藏取消苗商
const handleFavorite = async () => {
  let res = null
  if (merchantInfo.value.favoriteStoreState) {
    res = await unFavorite({
      type: 'store',
      relevanceId: merchantInfo.value?.userId
    })
  } else {
    res = await favorite({
      type: 'store',
      relevanceId: merchantInfo.value?.userId
    })
  }
  if (res.code === 200) {
    getMxInfo()
  }
}

// 收藏取消苗木
const handleStore = async () => {
  let res = null
  if (seedingInfo.value.favoritePushState == 1) {
    res = await unFavorite({
      type: 'push',
      relevanceId: seedingInfo.value?.id
    })
  } else {
    res = await favorite({
      type: 'push',
      relevanceId: seedingInfo.value?.id
    })
  }
  if (res.code === 200) {
    getDetailInfo()
  }
}

// 拨打电话
const handlePhone = async () => {
  if (phone.value) {
    bridge.phoneFunc({ value: phone.value })
  } else {
    await getPhone()
    bridge.phoneFunc({ value: phone.value })
  }
}
</script>

<style lang="scss" scoped>
.detail-container {
  height: 100vh;
  background: #f5f5f5;
  display: flex;
  flex-direction: column;

  :deep(.van-nav-bar) {
    background: #fff;
    height: 44px;

    &::after {
      border: none;
    }

    .van-nav-bar__title {
      color: #222;
      font-weight: 400;
    }

    .van-icon {
      color: #222;
    }
  }

  .main {
    flex: 1;
    overflow-y: auto;
  }

  .swiper-section {
    position: relative;
    height: 375px;
    background: #fff;

    .swiper {
      height: 100%;

      :deep(.van-image) {
        width: 100%;
        height: 100%;
      }
    }

    .swiper-count {
      position: absolute;
      right: 12px;
      bottom: 12px;
      padding: 4px 8px;
      border-radius: 10px;
      background: rgba(0, 0, 0, 0.3);
      color: #fff;
      font-size: 12px;
    }
  }

  .content-section {
    .price-section {
      padding: 12px;
      background: #fff;
      margin-bottom: 10px;

      .price {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 10px;

        .symbol {
          font-size: 13px;
          color: #f1641a;
        }

        .amount {
          font-size: 20px;
          font-weight: 400;
          color: #f1641a;
        }

        .relese-time {
          font-size: 10px;
          color: #999;
        }
      }

      .title {
        font-size: 16px;
        color: #222;
        line-height: 22px;
      }
    }

    .spec-section {
      padding: 12px;
      background: #fff;
      margin-bottom: 10px;

      .section-title {
        font-size: 15px;
        color: #222;
        margin-bottom: 12px;
      }

      .spec-list {
        .spec-item {
          display: flex;
          align-items: center;
          font-size: 14px;
          line-height: 22px;
          margin: 10px 0;

          .label {
            color: #666;
            margin-right: 8px;
            flex: 0 0 100px;
          }

          .value {
            color: #222;
          }
        }
      }
    }

    .merchant-section {
      padding: 12px;
      background: #fff;
      margin-bottom: 10px;

      .merchant-info {
        display: flex;
        align-items: center;
        .avatar {
          position: relative;
          p {
            position: absolute;
            top: 43px;
            font-size: 10px;
            color: #fff;
            width: 50px;
            height: 16px;
            background: #c8933b;
            border-radius: 15px;
            text-align: center;
            line-height: 16px;
          }
        }

        .info {
          flex: 1;
          margin-left: 10px;

          .name {
            font-size: 15px;
            color: #222;
            margin-bottom: 4px;
            font-weight: 400;
            line-height: 20px;
          }

          .tags {
            display: flex;
            gap: 4px;
            margin-bottom: 4px;

            :deep(.van-tag) {
              color: #d65858;
              border-color: #d65858;
              height: 18px;
              font-size: 10px;
            }

            .crown {
              color: #d65858;
              border-color: #d65858;
            }

            .verify {
              color: #c8933b;
              border-color: #c8933b;
            }

            :deep(.van-tag--plain:before) {
              border-radius: 15px;
            }
          }
        }

        :deep(.van-button) {
          height: 24px;
          color: #666;
          font-size: 12px;
        }
        :deep(.van-button--primary) {
          border-color: #666 !important;
        }
      }

      .merchant-desc {
        padding: 12px 0;
        font-size: 13px;
        color: #666;
        font-weight: 400;
        line-height: 20px;
        .desc-item {
          margin-bottom: 6px;
        }
      }

      .merchant-fans {
        padding: 10px 0;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .left {
          font-size: 12px;
          color: #666;
          .van-icon {
            margin-right: 5px;
            &.van-icon-like {
              color: #32bc05;
            }
          }
        }
        .right {
          display: flex;
          align-items: center;
          gap: 12px;
          .van-button {
            padding: 0 12px;
            background-color: #32bc05;
            width: 80px;
            height: 28px;
            color: #fff;
            font-size: 13px;
          }
          a {
            color: #fff;
          }
        }
      }
    }

    .dynamic-section {
      padding: 12px;
      background: #fff;

      .section-title {
        font-size: 15px;
        color: #222;
        margin-bottom: 12px;
      }

      .dynamic-list {
        .dynamic-item {
          padding: 12px 0;
          border-bottom: 1px solid #f5f5f5;

          .user-info {
            display: flex;
            align-items: center;
            font-size: 14px;
            gap: 10px;

            .name {
              margin-bottom: 8px;
              color: #222;
              font-size: 14px;
              span {
                margin-left: 10px;
                color: #999;
              }
            }
            .time {
              font-size: 11px;
              color: #999;
            }
          }
        }
      }
    }
  }

  .footer-section {
    height: 50px;
    background: #fff;
    display: flex;
    align-items: center;
    padding: 0 12px;
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.05);
    gap: 25px;
    .left {
      display: flex;
      align-items: center;

      .btn-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        font-size: 12px;
        color: #666;
      }
    }

    .right {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      gap: 12px;

      :deep(.van-button) {
        flex: 1;
        height: 36px;
        border-radius: 18px;
        font-size: 14px;
        background: #32bc05;
        color: #fff;
        border: none;
      }

      a {
        color: #fff;
      }
    }

    .van-icon {
      margin-right: 5px;
      &.van-icon-like {
        color: #32bc05;
      }
    }
  }
}
</style>
